package com.yaozheng.vocationaltraining.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.view.Display;
import android.view.WindowManager;

public class ControlsUtils {
    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 得到当前屏幕大小
     *
     * @param activity 当前上下文
     * @return 返回当前屏幕的宽与高 [0]=height [1]=width
     */
    public static int[] getCurrScreenSize1(Activity activity) {
        WindowManager manage = activity.getWindowManager();
        Display display = manage.getDefaultDisplay();
        int sizes[] = {display.getHeight(), display.getWidth()};
        return sizes;

    }

    /**
     * 得到当前屏幕大小
     *
     * @param activity 当前上下文
     * @return 返回当前屏幕的宽与高 height
     */
    public static int getCurrScreeHeight(Activity activity) {
        WindowManager manage = activity.getWindowManager();
        Display display = manage.getDefaultDisplay();
        return display.getHeight();

    }

    /**
     * 得到当前屏幕大小
     *
     * @param activity 当前上下文
     * @return 返回当前屏幕的宽与高 width
     */
    public static int getCurrScreeWidth(Activity activity) {
        WindowManager manage = activity.getWindowManager();
        Display display = manage.getDefaultDisplay();
        return display.getWidth();
    }

    /**
     * 得到当前版本号
     *
     * @param context
     * @return
     */
    public static String getAppVersionName(Context context) {
        String versionName = "";
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionName = pi.versionName;
            if (versionName == null || versionName.length() <= 0) {
                return "";
            }
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
        return versionName;
    }

    /**
     * 得到当前版本号
     *
     * @param context
     * @return
     */
    public static int getAppVersionCode(Context context) {
        int versionCode = 0;
        try {
            PackageManager pm = context.getPackageManager();
            PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
            versionCode = pi.versionCode;
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
        }
        return versionCode;
    }
}
